﻿using System.Collections.Generic;
using System.Windows.Forms;
using ReportWizard.DataAccess;

namespace ReportWizard.Comparers
{
    class UserInfoComparer : IComparer<UserInfo>
    {
        private UserInfo.SortingFields MemberName { get; set; }
        private SortOrder SortingOrder { get; set; }
        
        public UserInfoComparer()
        {
            MemberName = UserInfo.SortingFields.DateCreated;
            SortingOrder = SortOrder.None;
        }

        public UserInfoComparer(UserInfo.SortingFields memberName, SortOrder sortingOrder)
        {
            MemberName = memberName;
            SortingOrder = sortingOrder;
        }

        public int Compare(UserInfo x, UserInfo y)
        {
            int compareValue;

            switch (MemberName)
            {
                case UserInfo.SortingFields.FirstName:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.FirstName.CompareTo(y.FirstName) : y.FirstName.CompareTo(x.FirstName);
                        break;
                    }
                case UserInfo.SortingFields.LastName:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.LastName.CompareTo(y.LastName) : y.LastName.CompareTo(x.LastName);
                        break;
                    }
                case UserInfo.SortingFields.Login:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Login.CompareTo(y.Login) : y.Login.CompareTo(x.Login);
                        break;
                    }
                case UserInfo.SortingFields.DateCreated:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.DateCreated.CompareTo(y.DateCreated) : y.DateCreated.CompareTo(x.DateCreated);
                        break;
                    }
                case UserInfo.SortingFields.Admin:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Admin.CompareTo(y.Admin) : y.Admin.CompareTo(x.Admin);
                        break;
                    }
                case UserInfo.SortingFields.Active:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.Active.CompareTo(y.Active) : y.Active.CompareTo(x.Active);
                        break;
                    }
                default:
                    {
                        compareValue = SortingOrder == SortOrder.Ascending ? x.DateCreated.CompareTo(y.DateCreated) : y.DateCreated.CompareTo(x.DateCreated);
                        break;
                    }
            }

            if (compareValue == 0)
            {
                return SortingOrder == SortOrder.Ascending
                                   ? x.DateCreated.CompareTo(y.DateCreated)
                                   : y.DateCreated.CompareTo(x.DateCreated);
            }
            return compareValue;
        }
    }
}
